package zy.cui.demozuul.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class PreFilter extends ZuulFilter {


    @Override
    public String filterType() {

        return "pre" ;
    }

    @Override
    public int filterOrder() {

        return 0 ;
    }

    @Override
    public boolean shouldFilter() {

        return true;
    }

    @Override
    public Object run() {


        RequestContext context = RequestContext.getCurrentContext() ;
        HttpServletRequest request = context.getRequest() ;

        String token = request.getHeader("token") ;
        String url = request.getRequestURI() ;

        //访问地址是登录就不验证不拦截
        if(url.indexOf("login")>-1) {
            return "pass" ;
        }

        if(null == token) {
            context.setSendZuulResponse(false); //请求终止响应
            context.setResponseStatusCode(401); //必须是401
            context.setResponseBody("you don't have quanxian");
            return "Access denied ssss" ;
        }


        return "pass";
    }
}
